//
//  IHLoginManager.swift
//  InternetHospital
//
//  Created by Jekin on 2019/1/11.
//  Copyright © 2019 ZhuJiangChilink. All rights reserved.
//

import UIKit
import RxSwift

enum IHUserStateError : Error {
    case loginError(NSError)
    case logoutError(NSError)
}

private let shareInstance = IHLoginManager()
final class IHLoginManager: NSObject {
    private var userModel : IHUserModel?
    var user : IHUserModel {
        get {
            return self.userModel!
        }
    }
    
    class var shareManager : IHLoginManager {
        return shareInstance
    }
    
    //登录
    @discardableResult
    func login(_ username: String!,_ password:String!)->Single<IHUserModel> {
        
        return Single<IHUserModel>.create { (observer) -> Disposable in
            
            return Disposables.create{}
        }
    }
    
    //登出
    @discardableResult
    func logout()->Completable {
        return Completable.create(subscribe: {[weak self] completable -> Disposable in
            self?.userModel = nil
            if self?.userModel != nil {
                completable(.error(IHUserStateError.logoutError(NSError(domain: "退出失败", code: -1, userInfo: nil))))
            }else {
              completable(.completed)
            }
            return Disposables.create()
        })
    }
    
}
